Styling the Currency and Numeric Text Boxes
The CurrencyTextBox control allows you to define a separate style for negative values in order to highlight them to users. This is actually a special case of a general conditional styling mechanism for numeric text boxes that you can use in your own custom templates.
Styling Negative Currency Values
To customize the display of negative values, use the NegativeStyle property of the CurrencyTextBox. In the default template, this style is applied to a TextBox, so the TargetType of the style should be TextBox.
Conditional Styling
You may have more specialized requirements for conditional styling than simple negative value highlighting. You can implement these using standard WPF templating and triggering techniques as follows. The account below applies to all numeric text boxes (IntegerTextBox, NumericTextBox, CurrencyTextBox).
- Implement IValueConverter to encapsulate your conditional logic. For example, the SignConverter which is used by CurrencyTextBox returns an enum classifying the value as positive, negative or zero. A converter for a mathematical educational application might classify numbers as prime or composite.
- Create a custom template for the control. This must include, at minimum, a TextBox with the name “PART_TextBox”.
- Create a DataTrigger on the custom template which binds to the Value property and uses your custom converter. Your binding should specify RelativeSource Self as the binding source. So your binding will look something like this: <DataTrigger Binding=”{Binding Value, RelativeSource={RelativeSource Self}, Converter={StaticResource …}}” ... >. Create setters for this trigger in the normal way. For example, the default template of the CurrencyTextBox has a DataTrigger for when the result of applying SignConverter to Value is Sign.Negative, which sets the Style of the text box to the user-specified NegativeStyle. The educational application might play different animations depending on the type of number.
For an example, see IntegerTextBoxStyling.xaml in the Elements sample.
Templating
You can create custom templates for the numeric text boxes (CurrencyTextBox, NumericTextBox and IntegerTextBox) using the standard WPF approach. Any custom template you create must contain the following elements:
PART_TextBox | The numeric text boxes display output and receives input through this part. It must be a TextBox (or a type derived from TextBox). If this part is missing, or is not a TextBox, the numeric editor will not be able to display output or receive input. |